﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PolePositionMobileUtilityEntity;
using PolePositionMobileUtilityGeneral;
using PolePositionMobileUtilityLogic;
using System.IO;

namespace PolePositionMobileUtilityApplication
{
    public partial class frmHome : frmBase
    {
        private frmUtility m_FrmInspectionEditor;
        private FrmAbout m_FrmAbout;
        private frmSetting m_FrmSettings;

        public frmHome()
        {
            Setup();
            InitializeComponent();
            m_FrmInspectionEditor = new frmUtility();
            m_FrmAbout = new FrmAbout();
            m_FrmSettings = new frmSetting();
        }

        private void frmHome_Load(object sender, EventArgs e)
        {
            SetTitle("PPI | Home");

            #region Config form
            SetLButton("New");
            SetRButton("Menu");
            SetLContextMenu(contextMenuInspections);
            LButtonMenuXOffset = 1;
            LButtonMenuYOffset = 40;
            SetRContextMenu(contextMenuGeneral);
            RButtonMenuXOffset = 4;
            RButtonMenuYOffset = 60;
            #endregion
            grdInspections.ConfigureGrid();
        }

        #region Menu Events

        private void contextMenuInspections_Popup(object sender, EventArgs e)
        {
            mitDuplicateLast.Enabled = (grdInspections.GetLastInspection() != Int32.MinValue);
        }

        private void mitNew_Click(object sender, EventArgs e)
        {
            if (!m_GPS.HasSolution)
            {
                DialogResult _result = MessageBox.Show("There is no solution available.\n Do you still want to continue?", "PPI Location", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (_result == DialogResult.No)
                {
                    return;
                }            
            }

            m_FrmInspectionEditor.SetSelectedTab(ResultTabs.General);
            m_FrmInspectionEditor.SetInspection(Int32.MinValue);
            this.Hide();
            m_FrmInspectionEditor.ShowDialog();
            this.Show();
            grdInspections.ReloadGrid();
        }

        private void mitDuplicateLast_Click(object sender, EventArgs e)
        {
            if (!m_GPS.HasSolution)
            {
                DialogResult _result = MessageBox.Show("There is no solution available.\n Do you still want to continue?", "PPI Location", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (_result == DialogResult.No)
                {
                    return;
                }
            }
            m_FrmInspectionEditor.SetSelectedTab(ResultTabs.General);
            m_FrmInspectionEditor.SetInspection(grdInspections.GetLastInspection());
            m_FrmInspectionEditor.SetIsDuplication(true);
            this.Hide();
            m_FrmInspectionEditor.ShowDialog();
            this.Show();

            grdInspections.ReloadGrid();
        }

        private void mitCreateBackup_Click(object sender, EventArgs e)
        {
            this.Hide();
            SaveFileDialog saveFile = new SaveFileDialog();
            Application.DoEvents();
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                this.Show();
                this.pnlBackup.Visible = true;
                DirectoryInfo _DestinationDirectory = new DirectoryInfo(saveFile.FileName);

                Application.DoEvents();
                string[] _AppDataFolderFiles = Directory.GetFiles(PPIConstants.K_AppDataFolder);
                string[] _ImagesFolderFiles = Directory.GetFiles(PPIConstants.K_ImagesFolder);
                prgBackup.Maximum = _AppDataFolderFiles.Length + _ImagesFolderFiles.Length;
                prgBackup.Minimum = 0;
                prgBackup.Value = 0;
                lblBackup.Text = "Proccessing...\r\n" + prgBackup.Value.ToString() + " of " + prgBackup.Maximum.ToString();
                pnlBackup.Visible = true;
                pnlBackup.BringToFront();
                Application.DoEvents();
                if (!Directory.Exists(_DestinationDirectory.FullName + Path.DirectorySeparatorChar + PPIConstants.K_BackupFolder))
                    Directory.CreateDirectory(_DestinationDirectory.FullName + Path.DirectorySeparatorChar + PPIConstants.K_BackupFolder);

                string _Destination = _DestinationDirectory.FullName + Path.DirectorySeparatorChar + PPIConstants.K_BackupFolder + Path.DirectorySeparatorChar + DateTime.Now.ToString("yyyyMMddHHmmss") + "\\";
                if (!Directory.Exists(_Destination + "Images\\"))
                    Directory.CreateDirectory(_Destination + "Images\\");
                foreach (string _Element in _AppDataFolderFiles)
                {
                    File.Copy(_Element, _Destination + Path.GetFileName(_Element), true);
                    prgBackup.Value++;
                    lblBackup.Text = "Proccessing...\r\n" + prgBackup.Value.ToString() + " of " + prgBackup.Maximum.ToString();
                    Application.DoEvents();
                }
                foreach (string _Element in _ImagesFolderFiles)
                {
                    File.Copy(_Element, _Destination + "Images\\" + Path.GetFileName(_Element), true);
                    prgBackup.Value++;
                    lblBackup.Text = "Proccessing...\r\n" + prgBackup.Value.ToString() + " of " + prgBackup.Maximum.ToString();
                    Application.DoEvents();
                }

                pnlBackup.Visible = false;
                Application.DoEvents();
                if (MessageBox.Show("Backup done." + Environment.NewLine + "Clear database?", PPIConstants.K_AppName, MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    Application.DoEvents();
                    if (Directory.Exists(PPIConstants.K_ImagesFolder))
                        Directory.Delete(PPIConstants.K_ImagesFolder, true);
                    Directory.CreateDirectory(PPIConstants.K_ImagesFolder);

                    LUtilityInspectionResult.ClearDatabase();
                    Application.DoEvents();
                    grdInspections.ConfigureGrid();
                }
            }else
            {
                this.Show();
            }
            SetCorrectOrder();
        }

        private void mitSettings_Click(object sender, EventArgs e)
        {
            m_FrmSettings.ShowDialog();
        }

        private void mitAbout_Click(object sender, EventArgs e)
        {
            m_FrmAbout.ShowDialog();
        }

        #endregion

        #region Grid Events

        private void grdInspections_ClickedAddImage(object sender, PolePositionMobileUtilityGeneral.Events.GridEventArgs e)
        {
            m_FrmInspectionEditor.SetInspection(e.Id);
            m_FrmInspectionEditor.SetIsDuplication(false);
            m_FrmInspectionEditor.SetSelectedTab(ResultTabs.InspectionImages);
            this.Hide();
            m_FrmInspectionEditor.ShowDialog();
            this.Show();

        }

        private void grdInspections_ClickedDelete(object sender, PolePositionMobileUtilityGeneral.Events.GridEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            LUtilityInspectionResult.Delete(LUtilityInspectionResult.GetById(e.Id));
            grdInspections.ReloadGrid();
            Cursor.Current = Cursors.Default;
        }

        private void grdInspections_ClickedDuplicate(object sender, PolePositionMobileUtilityGeneral.Events.GridEventArgs e)
        {
            if (!m_GPS.HasSolution)
            {
                DialogResult _result = MessageBox.Show("There is no solution available.\n Do you still want to continue?", "PPI Location", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (_result == DialogResult.No)
                {
                    return;
                }
            }
            m_FrmInspectionEditor.SetSelectedTab(ResultTabs.General);
            m_FrmInspectionEditor.SetInspection(e.Id);
            m_FrmInspectionEditor.SetIsDuplication(true);
            this.Hide();
            m_FrmInspectionEditor.ShowDialog();
            this.Show();

            grdInspections.ReloadGrid();
        }

        private void grdInspections_ClickedEdit(object sender, PolePositionMobileUtilityGeneral.Events.GridEventArgs e)
        {
            m_FrmInspectionEditor.SetSelectedTab(ResultTabs.General);
            m_FrmInspectionEditor.SetInspection(e.Id);
            m_FrmInspectionEditor.SetIsDuplication(false);
            this.Hide();
            m_FrmInspectionEditor.ShowDialog();
            this.Show();

            grdInspections.ReloadGrid();
        }
        #endregion

        private void mitExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}